function getxhr(){
	var xhr = false;
	if(window . XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}else{
		xhr = window . ActiveXObject('Microsoft . XMLHttp');
	}
	return xhr;
}

window.onload=function(){
	var fm=document.getElementsByTagName('form');
	var inp=fm[1].getElementsByTagName('input');
	inp[0].onblur=function(){
		var patt=/^[\w\u4E00-\u9FA5]{4,16}$/;
		if(!patt.test(this.value)){
			this.style.borderColor='red';
			document.getElementById('cu').innerHTML='<font color="red">4-16位中英文</font>';
		}else{
			this.style.borderColor='';
			var uri='http://localhost/ajax.php?username='+this.value;
			var xhr=getxhr();
			xhr.open('GET',uri,true);
			xhr.onreadystatechange=function (){
				if(xhr.readyState==4){
					if(xhr.responseText==0){
						document.getElementById('cu').innerHTML='<font color="green">用户名可用</font>';
					}else{
						document.getElementById('cu').innerHTML='<font color="red">用户名已被用</font>';
					}
				}
			}
		}
		xhr.send(null);
	}
	
	inp[1].onblur=function(){
		var patt=/^[\w\u4E00-\u9FA5]{4,24}$/;
		var pwd=document.getElementById('pwd');
		if(this.value==''){
			pwd.innerHTML='<font color="red">密码不能为空</font>';
		}else if(!patt.test(this.value)){
			pwd.innerHTML='<font color="red">密码长度4-24位</font>';
		}else{
			pwd.innerHTML='<font color="green">密码可用</font>';
		}
	}
	inp[2].onblur=function(){
		if(inp[1].value!=this.value){
			document.getElementById('repwd').innerHTML='<font color="red">两次密码不一致</font>';
		}else{
			if(inp[2].value == ''){
				document.getElementById('repwd').innerHTML='<font color="red">密码不能为空</font>';
			}else{
				document.getElementById('repwd').innerHTML='<font color="green">两次密码一致</font>';
			}
		}
	}
	inp[3].onblur=function(){
		var patt=/^[a-z0-9]\w*(\.[a-z0-9]+)*@[a-z0-9][a-z0-9-]*(\.[a-z0-9][a-z0-9-]*)+$/i;
		var email=document.getElementById('email');
		if(!patt.test(this.value)){
			email.innerHTML='<font color="red">请输入正确的邮箱</font>';
		}else{
			email.innerHTML='<font color="green">邮箱可用</font>';
		}
	}
	
	inp[4].onblur=function(){
		var code = document.getElementById('code');
		if(inp[4].value == ''){
			code.innerHTML='<font color="red">请输入验证码</font>';
		}else{
			var xhr = getxhr();
			var url = 'user.php?act=reg_ok&val=' + inp[4].value;
			xhr.open('GET', url, true);
			xhr.onreadystatechange = function(){
				if(xhr.readyState == 4){
					if(xhr.responseText == 1){
						code.innerHTML='<font color="green">验证码正确</font>';
					}else if(xhr.responseText == 0){
						code.innerHTML='<font color="red">验证码错误</font>';
					}else if(xhr.responseText == 2){
						code.innerHTML='<font color="red">异常</font>';
					}
				}
			}
			xhr.send(null);
		}
	}

	fm[1].onsubmit=function(ev){
		if(document.getElementsByName('username')[0].value==''){
			alert('用户名不能为空');
			return false;
		}
		var pwd=document.getElementsByName('passWord')[0];
		var repwd=document.getElementsByName('rePassWord')[0];
		if(pwd.value==''||repwd.value==''){
			alert('密码不能为空');
			return false;
		}
		if(pwd.value!=repwd.value){
			alert('两次密码不一致');
			return false;
		}
		var patt=/^[a-z0-9]\w*(\.[a-z0-9]+)*@[a-z0-9][a-z0-9-]*(\.[a-z0-9][a-z0-9-]*)+$/i;
		if(!patt.test(document.getElementsByName('email')[0].value)){
			alert('邮箱不正确');
			return false;
		}
	}
	
	var safecode = document . getElementById('safecode');
	safecode . onclick = function (){
		safecode . src = "../templates/front/images/captcha.php?act="+Math.random(); 
	}
	
}


